package org.lex.keytool.core.task;

import org.lex.keytool.core.DName;
import org.lex.keytool.core.DName.Name;
import org.lex.keytool.core.runner.NoneForkTaskRunner;

public class KtTest {
	public static String pass = "123456";
	public static String keystore = "C:\\a.keys";
	public static String cerfile = "C:\\a.cer";
	public static String alias = "kttest";
	public static String storetype = "jks";

	public static String publickey = "C:\\a.public";
	public static String privatekey = "C:\\a.private";

	public static void main(String[] args) throws Exception {
		Task[] task = { testCreatePublicAndPrivateKeyTask(), testExportCertificateTask(), testImportCertificateTask() };
		NoneForkTaskRunner run = new NoneForkTaskRunner();
		for (Task t : task) {
			System.err.println("=======Running " + t.getClass().getSimpleName() + "========\n");
			run.run(t);
		}
	}

	static Task testCreatePublicAndPrivateKeyTask() throws Exception {
		CreatePublicAndPrivateKeyTask task = new CreatePublicAndPrivateKeyTask();
		task.setStorePassword(pass);
		task.setKeyPassword(pass);
		task.setKeyStore(keystore);
		task.setAlias(alias);
		task.setStoreType(storetype);
		DName dn = task.getDname();
		dn.set(Name.C, "C");
		dn.set(Name.CN, "CN");
		dn.set(Name.L, "L");
		dn.set(Name.O, "O");
		dn.set(Name.OU, "OU");
		dn.set(Name.S, "S");
		return task;
	}

	static Task testExportCertificateTask() throws Exception {
		ExportCertificateTask task = new ExportCertificateTask();
		task.setKeyStore(keystore);
		task.setStorePassword(pass);
		task.setFile(cerfile);
		task.setStoreType(storetype);
		task.setAlias(alias);
		return task;
	}

	static Task testImportCertificateTask() throws Exception {
		ImportCertificateTask task = new ImportCertificateTask();
		task.setAlias(alias);
		task.setFile(cerfile);
		task.setKeyPassword(pass);
		task.setKeyStore(publickey);
		task.setStorePassword(pass);
		task.setStoreType(storetype);
		return task;
	}
}
